package jupl.file.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

/**
 * @author Denis Migol
 * 
 */
public class FTPFileUploader {

	private final FTPClient client = new FTPClient();

	/**
	 * 
	 * @param hostname
	 * @param username
	 * @param password
	 * @param file
	 * @throws IOException
	 */
	public void upload(final String hostname, final String username, final String password, final File file)
			throws IOException {
		FileInputStream fis = null;
		try {
			client.connect(hostname);
			client.login(username, password);
			client.setFileType(FTP.BINARY_FILE_TYPE);

			fis = new FileInputStream(file);

			client.storeFile(file.getName(), fis);
			client.logout();
		} finally {
			if (fis != null) {
				fis.close();
			}
			if (client.isConnected()) {
				client.disconnect();
			}
		}
	}

}
